// $codepro.audit.disable
/**
 * SystemAdmin's UI screen.
 * @author Suren Nihalani
 * 
 */
package edu.gatech.coc.betameisters.userinterfaces;

import javax.swing.JPanel;

import javax.swing.DefaultComboBoxModel;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JTextField;

import edu.gatech.coc.betameisters.main.MainPanel;
import edu.gatech.coc.betameisters.people.*;
import edu.gatech.coc.betameisters.infoholders.Constants.UserType;
import javax.swing.JComboBox;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.table.DefaultTableModel;

// TODO: Auto-generated Javadoc
/**
 * The Class SystemAdminsUI.
 * 
 * @version $REVISION$
 * @author Suren Nihalani
 */
@SuppressWarnings("serial")
public class SystemAdminsUI extends JPanel {

	/** The admin. */
	private SystemAdmin theAdmin;

	/** The users table. */
	private final JTable usersTable;

	/** The search field. */
	private final JTextField searchField;
	private JTextField txtFirstName;
	private JTextField txtLastName;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private boolean isSearchingByUserName;
	private int selectedRow;

	@SuppressWarnings("unused")
	private MainPanel parent;

	/**
	 * Create the panel.
	 * 
	 * @param anAdmin
	 *            The admin who will be interacting with the screen.
	 */
	public SystemAdminsUI(SystemAdmin anAdmin, MainPanel p) {
		theAdmin = anAdmin;
		parent = p;
		final SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);

		final JLabel lblWelcome = new JLabel("Current User: "
				+ theAdmin.toString());
		springLayout.putConstraint(SpringLayout.NORTH, lblWelcome, 10,
				SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, lblWelcome, 10,
				SpringLayout.WEST, this);
		add(lblWelcome);

		final JButton logoutButton = new JButton("Logout");
		springLayout.putConstraint(SpringLayout.NORTH, logoutButton, -4,
				SpringLayout.NORTH, lblWelcome);
		springLayout.putConstraint(SpringLayout.EAST, logoutButton, -10,
				SpringLayout.EAST, this);
		add(logoutButton);

		usersTable = new JTable();
		springLayout.putConstraint(SpringLayout.NORTH, usersTable, 30,
				SpringLayout.SOUTH, lblWelcome);
		springLayout.putConstraint(SpringLayout.WEST, usersTable, 10,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.SOUTH, usersTable, -10,
				SpringLayout.SOUTH, this);
		springLayout.putConstraint(SpringLayout.EAST, usersTable, -205,
				SpringLayout.EAST, this);

		usersTable.setModel(new DefaultTableModel(new Object[25][3],
				new String[] { "Real Name", "Username", "Information" }));

		add(usersTable);

		searchField = new JTextField();
		springLayout.putConstraint(SpringLayout.WEST, searchField, 28,
				SpringLayout.EAST, usersTable);
		springLayout.putConstraint(SpringLayout.EAST, searchField, -10,
				SpringLayout.EAST, this);
		add(searchField);
		searchField.setColumns(10);

		final JComboBox createUserTypeComboBox = new JComboBox();
		springLayout.putConstraint(SpringLayout.NORTH, createUserTypeComboBox,
				97, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.EAST, createUserTypeComboBox,
				0, SpringLayout.EAST, logoutButton);
		createUserTypeComboBox.setModel(new DefaultComboBoxModel(
				new UserType[] { UserType.PATIENT, UserType.NURSE,
						UserType.DOCTOR, UserType.ADMIN }));
		add(createUserTypeComboBox);

		final JButton newUserButton = new JButton("New User");
		springLayout.putConstraint(SpringLayout.WEST, newUserButton, 28,
				SpringLayout.EAST, usersTable);
		springLayout.putConstraint(SpringLayout.EAST, newUserButton, -10,
				SpringLayout.EAST, this);
		springLayout.putConstraint(SpringLayout.NORTH, searchField, 29,
				SpringLayout.SOUTH, newUserButton);
		springLayout.putConstraint(SpringLayout.WEST, createUserTypeComboBox,
				75, SpringLayout.WEST, newUserButton);
		springLayout.putConstraint(SpringLayout.SOUTH, newUserButton, 150,
				SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.NORTH, newUserButton, 129,
				SpringLayout.NORTH, this);
		add(newUserButton);

		final JButton editUserButton = new JButton("Edit User");
		editUserButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (editUserButton.isEnabled()) {
					parent.setLocal(theAdmin.getCurrent());
					parent.setUser(theAdmin.getCurrent());
				}
			}
		});
		editUserButton.setEnabled(false);
		springLayout.putConstraint(SpringLayout.WEST, editUserButton, 28,
				SpringLayout.EAST, usersTable);
		springLayout.putConstraint(SpringLayout.EAST, editUserButton, -10,
				SpringLayout.EAST, this);
		add(editUserButton);

		final JButton deleteUserButton = new JButton("Delete User");
		deleteUserButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (deleteUserButton.isEnabled()) {
					String username = (String) usersTable.getValueAt(
							selectedRow, 0);
					if (theAdmin.deleteUser(username,
							(UserType) usersTable.getValueAt(selectedRow, 2))) {
						System.out.println("User was deleted froms system");
					} else {
						System.out.println("User was not deleted from system");
					}
					for (int i = 0; i < usersTable.getColumnCount(); i++) {
						usersTable.setValueAt("", selectedRow, i);
					}
					// could display an error window here
				}
			}
		});
		deleteUserButton.setEnabled(false);
		springLayout.putConstraint(SpringLayout.WEST, deleteUserButton, 28,
				SpringLayout.EAST, usersTable);
		springLayout.putConstraint(SpringLayout.EAST, deleteUserButton, -10,
				SpringLayout.EAST, this);
		springLayout.putConstraint(SpringLayout.SOUTH, editUserButton, -6,
				SpringLayout.NORTH, deleteUserButton);
		springLayout.putConstraint(SpringLayout.NORTH, deleteUserButton, 362,
				SpringLayout.NORTH, this);
		add(deleteUserButton);

		//DELETE
		final JButton resetPasswordButton = new JButton("Reset Password");
		resetPasswordButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (resetPasswordButton.isEnabled()) {
					theAdmin.searchUser(
							(String) usersTable.getValueAt(selectedRow, 0),
							(UserType) usersTable.getValueAt(selectedRow, 2),
							true);
					theAdmin.resetPassword();
				}
			}
		});
		resetPasswordButton.setEnabled(false);
		springLayout.putConstraint(SpringLayout.NORTH, resetPasswordButton,
				391, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, resetPasswordButton, 28,
				SpringLayout.EAST, usersTable);
		springLayout.putConstraint(SpringLayout.SOUTH, resetPasswordButton,
				-10, SpringLayout.SOUTH, this);
		springLayout.putConstraint(SpringLayout.EAST, resetPasswordButton, -10,
				SpringLayout.EAST, this);
		springLayout.putConstraint(SpringLayout.SOUTH, deleteUserButton, -6,
				SpringLayout.NORTH, resetPasswordButton);
		add(resetPasswordButton);
		//
		final JButton searchButton = new JButton("Search");
		searchButton.setEnabled(false);
		springLayout.putConstraint(SpringLayout.WEST, searchButton, 28,
				SpringLayout.EAST, usersTable);
		springLayout.putConstraint(SpringLayout.EAST, searchButton, -10,
				SpringLayout.EAST, this);
		springLayout.putConstraint(SpringLayout.NORTH, editUserButton, 43,
				SpringLayout.SOUTH, searchButton);
		add(searchButton);

		final JComboBox searchUserTypeComboBox = new JComboBox();
		springLayout.putConstraint(SpringLayout.WEST, searchUserTypeComboBox,
				120, SpringLayout.EAST, usersTable);
		springLayout.putConstraint(SpringLayout.SOUTH, searchUserTypeComboBox,
				-191, SpringLayout.SOUTH, this);
		springLayout.putConstraint(SpringLayout.EAST, searchUserTypeComboBox,
				-10, SpringLayout.EAST, this);
		springLayout.putConstraint(SpringLayout.SOUTH, searchField, -6,
				SpringLayout.NORTH, searchUserTypeComboBox);
		springLayout.putConstraint(SpringLayout.NORTH, searchButton, 34,
				SpringLayout.SOUTH, searchUserTypeComboBox);
		searchUserTypeComboBox.setModel(new DefaultComboBoxModel(
				new UserType[] { UserType.PATIENT, UserType.NURSE,
						UserType.DOCTOR, UserType.ADMIN }));
		add(searchUserTypeComboBox);

		txtFirstName = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, txtFirstName, -58,
				SpringLayout.NORTH, newUserButton);
		springLayout.putConstraint(SpringLayout.WEST, txtFirstName, 28,
				SpringLayout.EAST, usersTable);
		springLayout.putConstraint(SpringLayout.SOUTH, txtFirstName, -60,
				SpringLayout.SOUTH, newUserButton);
		add(txtFirstName);
		txtFirstName.setColumns(10);

		txtLastName = new JTextField();
		springLayout.putConstraint(SpringLayout.EAST, txtFirstName, -3,
				SpringLayout.WEST, txtLastName);
		springLayout.putConstraint(SpringLayout.WEST, txtLastName, 523,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.NORTH, txtLastName, 71,
				SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.EAST, txtLastName, 0,
				SpringLayout.EAST, newUserButton);
		add(txtLastName);
		txtLastName.setColumns(10);

		JLabel lblFirstName = new JLabel("First Name");
		springLayout.putConstraint(SpringLayout.WEST, lblFirstName, 28,
				SpringLayout.EAST, usersTable);
		springLayout.putConstraint(SpringLayout.SOUTH, lblFirstName, -6,
				SpringLayout.NORTH, txtFirstName);
		add(lblFirstName);

		JLabel lblLastName = new JLabel("Last Name");
		springLayout.putConstraint(SpringLayout.WEST, lblLastName, 0,
				SpringLayout.WEST, txtLastName);
		springLayout.putConstraint(SpringLayout.SOUTH, lblLastName, -6,
				SpringLayout.NORTH, txtFirstName);
		add(lblLastName);

		JRadioButton rdbtnRealName = new JRadioButton("Real Name");
		rdbtnRealName.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				isSearchingByUserName = false;
				searchButton.setEnabled(true);
			}
		});
		buttonGroup.add(rdbtnRealName);
		springLayout.putConstraint(SpringLayout.NORTH, rdbtnRealName, 6,
				SpringLayout.SOUTH, searchField);
		springLayout.putConstraint(SpringLayout.WEST, rdbtnRealName, 6,
				SpringLayout.EAST, usersTable);
		add(rdbtnRealName);

		JRadioButton rdbtnUserName = new JRadioButton("User Name");
		rdbtnUserName.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				isSearchingByUserName = true;
				searchButton.setEnabled(true);
			}
		});
		buttonGroup.add(rdbtnUserName);
		springLayout.putConstraint(SpringLayout.NORTH, rdbtnUserName, 6,
				SpringLayout.SOUTH, rdbtnRealName);
		springLayout.putConstraint(SpringLayout.WEST, rdbtnUserName, 6,
				SpringLayout.EAST, usersTable);
		add(rdbtnUserName);

		JLabel lblUserName = new JLabel("User Name");
		springLayout.putConstraint(SpringLayout.WEST, lblUserName, 10,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.SOUTH, lblUserName, -6,
				SpringLayout.NORTH, usersTable);
		add(lblUserName);

		JLabel lblRealName = new JLabel("Real Name");
		springLayout.putConstraint(SpringLayout.WEST, lblRealName, 82,
				SpringLayout.EAST, lblUserName);
		springLayout.putConstraint(SpringLayout.SOUTH, lblRealName, -6,
				SpringLayout.NORTH, usersTable);
		add(lblRealName);

		JLabel lblSomething = new JLabel("Something");
		springLayout.putConstraint(SpringLayout.WEST, lblSomething, 85,
				SpringLayout.EAST, lblRealName);
		springLayout.putConstraint(SpringLayout.SOUTH, lblSomething, -6,
				SpringLayout.NORTH, usersTable);
		add(lblSomething);

		// Action Listeners
		usersTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				editUserButton.setEnabled(true);
				deleteUserButton.setEnabled(true);
				resetPasswordButton.setEnabled(true);
				selectedRow = usersTable.getSelectedRow();
			}
		});

		newUserButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				theAdmin.createUser(txtFirstName.getText(),
						txtLastName.getText(),
						(UserType) createUserTypeComboBox.getSelectedItem());
			}
		});

		searchButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (searchButton.isEnabled()) {
					java.util.List<String> aList = theAdmin.searchUser(
							searchField.getText(),
							(UserType) searchUserTypeComboBox.getSelectedItem(),
							isSearchingByUserName);
					if (aList.size() == 0) {
						for (int i = 0; i < usersTable.getRowCount(); i++) {
							usersTable.setValueAt("", i, 0);
							usersTable.setValueAt("", i, 1);
							usersTable.setValueAt("", i, 2);
						}
					}
					for (int i = 0; i < aList.size(); i++) {
						theAdmin.searchUser(aList.get(i),
								(UserType) searchUserTypeComboBox
										.getSelectedItem(), true);
						Person generic = theAdmin.getCurrent();
						usersTable.setValueAt(generic.getUserID(), i, 0);
						usersTable.setValueAt(generic.getLastName() + " "
								+ generic.getFirstName(), i, 1);
						usersTable.setValueAt(generic.getUserType(), i, 2);
					}
				}
			}
		});
	}

	/**
	 * Gets the users table.
	 * 
	 * @return the users table
	 */
	public JTable getUsersTable() {
		return usersTable;
	}

	public SystemAdmin getTheAdmin() {
		return theAdmin;
	}

	public void setTheAdmin(SystemAdmin theAdmin) {
		this.theAdmin = theAdmin;
		repaint();
	}
}
